/*
 * Copyright 2020 Vincent Galloy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vgalloy.gatlingjavaapi.api.dsl.http.wrapper;

import com.vgalloy.gatlingjavaapi.internal.util.ScalaHelper;
import com.vgalloy.gatlingjavaapi.internal.util.expression.Expression;
import io.gatling.http.request.builder.RequestBuilder;
import java.util.Map;
import java.util.function.Supplier;

/**
 * Created by Vincent Galloy on 24/02/2017.
 *
 * @author Vincent Galloy.
 */
public abstract class RequestBuilderWrapper<
        STRUCTURE extends RequestBuilder<STRUCTURE>,
        WRAPPER extends RequestBuilderWrapper<STRUCTURE, WRAPPER>>
    implements Supplier<STRUCTURE> {

  /**
   * Build a new instance of the wrapper
   *
   * @param newStructure the wrapped gatling structure
   * @return the new wrapper
   */
  protected abstract WRAPPER newInstance(STRUCTURE newStructure);

  public WRAPPER header(String name, String value) {
    return newInstance(get().header(name, Expression.of(value)));
  }

  public WRAPPER headers(Map<String, String> headers) {
    final scala.collection.immutable.Map<String, String> map = ScalaHelper.map(headers);
    return newInstance(get().headers(map));
  }

  public WRAPPER queryParam(String name, Object value) {
    return newInstance(get().queryParam(Expression.of(name), Expression.of(value)));
  }
}
